// JavaScript Document
$(document).ready(function(e){
	$('div.tab-content').css('height',(screen.availHeight-275));
	/** begin */
	_cek();
	 if(	 $('#tp_setoran').val()=='Register')
	 {
		 _hidden_row('no_member','none');
		 _hidden_row('nm_member','none');
	 }else{
		 _hidden_row('nm_member_r','none');
		 _hidden_row('no_member_r','none');
	 }
		 _get_register('R');
		 _get_register('D');
		 _get_member('R');
		 _get_member('D');
		 _get_kontraakun('');
		 $('#frm1').validationEngine();
		 $('#p-tp_simpanan').click(function(){});
		 $('#tp_setoran').change(function(){
			 if($(this).val()=='Registrasi'){
				 _hidden_row('no_member','none');
				 _hidden_row('nm_member','none');
				 _hidden_row('no_member_r','');
				 _hidden_row('nm_member_r','');
				 _get_register('R');
				 $('#ket_trans').val('Registrasi Anggota Baru');
				 _get_register('D');
				 $('#tp_simpanan').attr('readonly','readonly');
			 }else{
				 _hidden_row('no_member_r','none');
				 _hidden_row('nm_member_r','none');
				 _hidden_row('no_member','');
				 _hidden_row('nm_member','');
				 _get_member('R');
				 _get_member('D');
				 $('#tp_simpanan').removeAttr('disabled');
			 }
		 });
				 $('#tgl_trans').datepicker();
				 $('#tgl_trans').datepicker("option","dateFormat","dd/mm/yy");
				 /*$('#tgl_trans').datepicker({minDate:-30,maxDate:"+0Y"})*/;
				 tglNow('#tgl_trans');
				 formatAngka('jml_simpanan');
				 $('#no_member').change(function(){_get_detail($(this).val());});
				 $('#nm_member').live('change',function(){_get_detail($(this).val());});
				 $('#no_member_r').change(function(){_get_detaile($(this).val());});
				 $('#nm_member_r').live('change',function(){_get_detaile($(this).val());})
				 /*$('#d_Save').click(function(){})*/;
				 $('#b_Save').click(function(){($('#kd_akun').val().length>=5)?_simpan_data():jAlert('Kode Akun belum sesuai');});
				 $('#tp_simpanan').change(function(){
					 $('#ket_trans')
					 	.val('Setoran '+$('#tp_simpanan option:selected')
						.text()+'-'+ $('#nm_member')
						.val().toUpperCase());
				 });
				 $('#tgl_skr').datepicker();
				 $('#tgl_skr').datepicker("option","dateFormat","dd/mm/yy");
				 tglNow('#tgl_skr');
				 
		$('#posting').click(function(){publish();});
		($('#row_aktif').val()=='y')?
		$('#tp_setoran').val('Pinjaman').select():
		$('#tp_setoran').val('Simpanan').select();
		//$('#b_Save').click(function(){ _print_kwt('85');})
		$('#c_cari').click(function(){_show_list('1');})

});
/** end document ready; 
get data typeahead json format*/
function _print_kwt(id)
{
	$.post('print_kwitansi',{'id':id},
	function(result)
	{
		$('#prv').attr('src',result);
		$('#PrintPrev .modal-body').css({'width':(screen.availWidth-170),'height':(screen.availHeight-205),'overflow':'auto'});
		$('#PrintPrev')
			.css({'left':'25%','width':(screen.availWidth-150),'height':(screen.availHeight-150)})
			.modal('show');
	})
	
}
function _get_kontraakun(tp)
{
	$.post('kontraakun',{'st':tp},function(result){
		$('#kd_akun2').typeahead({source:$.parseJSON(result)});});
}

function _get_register(tp)
{
	$.post('getakunreg',{'st':tp},function(result){
		(tp=='R')?$('#no_member_r').typeahead({source:$.parseJSON(result)}):
		$('#nm_member_r').typeahead({source:$.parseJSON(result)});});
}
function _get_member(tp)
{
	$.post('getakunmember',{'st':tp},function(result){
		(tp=='R')?$('#no_member').typeahead({source:$.parseJSON(result)}):
		$('#nm_member').typeahead({source:$.parseJSON(result)});});
}
function _hidden_row(id,st){$('div.control-group#g_'+id).css({'display':st});}
/**get detail member name or member id*/
function _get_detail(fld)
{
	$.post('get_akun_detail',{'id':fld},function(result)
	{
		var r=$.parseJSON(result);
		$('#nm_member').val(r.nm_member);
		$('#no_member').val(r.id_reg);
		})
}
function _get_detaile(fld)
{
	$.post('get_akun_detail',{'id':fld},function(result){
		var r=$.parseJSON(result);
		$('#nm_member_r').val(r.nm_member);
		$('#no_member_r').val(r.id_reg);
		})
}
/**simpan data transaksi*/
function _simpan_data(){
	if($('#frm1').validationEngine('validate')){
		$.post('set_setoran',$('#frm1').serialize(),function(result)
		{
			var rst=result.split(':');
			$('#result').html(rst[0]).show().fadeOut(5000);
			$('#d_Save').click();tglNow('#tgl_trans');
			jConfirm('Print Tanda terima /kwintansi','ziebar alert',function(r)
			{
				if(r)
				{
					_print_kwt(rst[1]);
				}
			})
		})
	}
}
function _show_list(id)
{
	show_indicator('NewTable',8);
	$.post('get_list_member',$('#frm2').serialize(),
	function(result){
		$('#NewTable tbody').html(result);
		fixHeader('NewList',50,450);
		})
		}
function _aksi(id,aksi){_set_simpanan(id,aksi);}
function _set_simpanan(id,aksi)
{
	if(id.checked)
	{
		$.post('set_setoran_nt',{
			'id_member':aksi,
			'bln'	  :$('#bln').val(),
			'tahun'	:$('#tahun').val(),
			'id_cabang':$('#id_cabang').val()},
		function(result){})
	}else{
		$.post('del_setoran_nt',{
			'id_member':aksi,
			'bln'	  :$('#bln').val(),
			'tahun'	:$('#tahun').val()},
		function(result){})
	}
}

function publish()
{
	show_indicator2('txt');
	$.post('publish_setoran_nt',$('#frm2').serialize(),function(result)
		{
			$('#rst').html(result);_show_list('1');
			$('#txt').html('');
		});	
}
function _cek()
{
	var m=$('#rows').val();
	(m=='Simpan Pinjam')?
		$('#tp_setoran').val('Simpanan').select().attr('readonly','readonly'):
		$('#tp_setoran').removeAttr('readonly');
}